package com.kdtech.analyse.video;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.analyse.AnalyseNews;

public class YyrtvAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		// http://v.yyrtv.com/player.php?id=13154
		String[] regex = { "http://v.yyrtv.com/player.php\\?id=[0-9]*" };
		
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (!isDetailPage(urlMeta.getUrl())) {
		}
		NewsMeta video = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		video.setUrl(url);
		String title = null;
		String desc = null;
		Long date = 0l;

		Document doc = Jsoup.parse(html);
		/*
		 * 解析标题,播放次数
		 */
		String tmp = doc.select("div.play_right_xx").text();
		int pos = tmp.indexOf("播放次数：");
		if (pos != -1) {
			title = tmp.substring(0, pos).replace("现在播放的视频:", "");
			try {
				int parseInt = Integer.parseInt(tmp.substring(pos
						+ "播放次数：".length()));
				video.setClickNum(parseInt);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
			}
		}
		/*
		 * 解析时间
		 */
		String text = doc.select("div.play_sp_info2_a2").text();
		date = DateUtils.matchDate(text);

		if (StringUtils.isBlank(title)) {
		}

		video.setUpdateUrl(url);

		video.setTitle(title);
		video.setDate(date);
		return video;
	}

	private final static Pattern P_PATH = Pattern
			.compile("[0-9]{4}/[0-9]{2}/[0-9]{4}-[0-9]{2}-[0-9]*.html");

	
	public NewsMeta Update(NewsMeta meta) {

		try {
			if (meta != null) {
				String updateUrl = meta.getUpdateUrl();
				if (updateUrl != null) {
					String getHtml = CrawlHTML.GetHtml(updateUrl, "UTF-8");
					Document doc = Jsoup.parse(getHtml);
					/*
					 * 解析标题,播放次数
					 */
					String tmp = doc.select("div.play_right_xx").text();
					int pos = tmp.indexOf("播放次数：");
					if (pos != -1) {
						try {
							int parseInt = Integer.parseInt(tmp.substring(pos
									+ "播放次数：".length()));
							meta.setClickNum(parseInt);
							return meta;
						} catch (NumberFormatException e) {
							// TODO Auto-generated catch block
						}
					}
				}
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
		}
		return null;
	}

	public static void main(String[] args) {
		YyrtvAnalyse a = new YyrtvAnalyse();
		String url = "http://v.yyrtv.com/player.php?id=30754";
		if (a.isDetailPage(url)) {
			UrlMeta meta = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = a.parserHtml(meta);
			System.out.println(parserHtml);
		} else {
			System.out.println("不匹配规则");
		}

	}
	
}
